﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;
using Process;

namespace QuanLyDiem
{
    public partial class frmSinhVien_Them : Form
    {
        private static frmSinhVien_Them frm = null;
        private BoMon bomon = new BoMon();
        private Lop lop = new Lop();
        private SinhVien sinhvien = new SinhVien();

        private frmSinhVien_Them()
        {
            InitializeComponent();
        }

        private void frmSinhVien_Them_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        public static frmSinhVien_Them Create()
        {
            return Create(false);
        }

        public static frmSinhVien_Them Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmSinhVien_Them();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            return frm;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmSinhVien_Them_Load(object sender, EventArgs e)
        {
            cmbBoMon.DataSource = bomon.ListName();
            cmbLop.DataSource = lop.ListName(cmbBoMon.Text);
            txtMaSV.Text = sinhvien.MaSinhVien(cmbLop.Text);
            txtTenSV.Focus();
        }

        private void cmbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbLop.DataSource = lop.ListName(cmbBoMon.Text);
            if (cmbLop.Items.Count <= 0)
            {
                txtMaSV.Text = "";
            }
        }

        private void cmbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaSV.Text = sinhvien.MaSinhVien(cmbLop.Text);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (cmbBoMon.Items.Count > 0)
            {
                cmbBoMon.SelectedIndex = 0;
            }
            txtTenSV.Text = "";
            txtQueQuan.Text = "";
            rdNam.Checked = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTenSV.Text))
            {
                SinhVien sv = new SinhVien();
                sv.ThongTin.Ten = txtTenSV.Text.Trim();
                sv.ThongTin.Lop = cmbLop.Text;
                sv.ThongTin.BoMon = cmbBoMon.Text;
                sv.ThongTin.QueQuan = txtQueQuan.Text.Trim();
                if (rdNam.Checked)
                {
                    sv.ThongTin.GioiTinh = "Nam";
                }
                else
                {
                    sv.ThongTin.GioiTinh = "Nữ";
                }
                sv.ThongTin.NgaySinh = dtpNgaySinh.Value;
                int check = sv.Add();
                if (check > 0)
                {
                    MessageBox.Show("Đã thêm thành công", "Thông báo", MessageBoxButtons.OK);
                    btnHuy.PerformClick();
                }
                else
                {
                    MessageBox.Show("Có vấn đề khi thêm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Phải nhập tên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void txtTenSV_Leave(object sender, EventArgs e)
        {
            txtTenSV.Text = xString.Name(txtTenSV.Text);
        }
        
    }
}
